/**
 * 提交回复
 */
function post() {
    var questionId = $("#question_id").val();
    var content = $("#comment_content").val();

    comment2target(questionId, 1, content);
}

/**
 * 二级评论
 * @param targetId
 * @param type
 * @param content
 */
function comment2target(targetId, type, content) {
    if (!content) {
        alert("输入内容不能为空！！！");
        return;
    }
    $.ajax({
        type: "POST",
        url: "/comment",
        contentType: 'application/json',
        data: JSON.stringify({
            "parentID": targetId,
            "content": content,
            "type": type
        }),
        success: function (response) {
            if (response.code == 200) {
                window.location.reload();
            } else {
                if (response.code == 2003) {
                    var isAccepted = confirm(response.message);
                    if (isAccepted) {
                        window.open("https://github.com/login/oauth/authorize?client_id=c2fd17e523182ce1fbd9&redirect_uri=http://localhost:8080/githubCallback&scope=user&state=1");
                        window.localStorage.setItem("closable", true);
                    }
                } else {
                    alert(response.message);
                }
            }
            console.log(response);
        },
        dataType: "json"
    });
}

function comment(e) {
    var commentId = e.getAttribute("data-id");
    var content = $("#input-" + commentId).val();
    comment2target(commentId, 2, content);
}

/*
* 展开二级评论
* */
function collapseComment(e) {
    var id = e.getAttribute("data-id");
    var comments = $("#comment-" + id);

    //获取二级评论展开的状态
    var collapse = e.getAttribute("data-collapse");
    if (collapse) {
        //折叠二级评论
        comments.removeClass("in");
        e.removeAttribute("data-collapse");
        e.classList.remove("active");
    } else {
        var subCommentContainer = $("#comment-" + id);
        if (subCommentContainer.children().length != 1) {
            //展开二级评论
            comments.addClass("in");
            //标记二级评论展开状态
            e.setAttribute("data-collapse", "in");
            e.classList.add("active");
        } else {
            $.getJSON("/comment/" + id, function (data) {
                $.each(data.data.reverse(), function (index, comment) {

                    //左边
                    var mediaLeftElement = $("<div/>", {
                        "class": "media-left"
                    }).append($("<img/>", {
                        "class": "media-object img-rounded",
                        "src": comment.user.avatarUrl
                    }));

                    var mediaBodyElement = $("<div/>", {
                        "class": "media-body"
                    }).append($("<h6/>", {
                        "class": "media-heading",
                        html: comment.user.name
                    })).append($("<div/>", {
                        html: comment.content
                    })).append($("<div/>", {
                        "class": "menu"
                    }).append($("<span/>", {
                        "class": "pull-right",
                        html: moment(comment.gmtCreate).format('YYYY-MM-DD')
                    })));

                    //整体
                    var mediaElement = $("<div/>", {
                        "class": "media"
                    }).append(mediaLeftElement).append(mediaBodyElement);


                    var commentElement = $("<div/>", {
                        "class": "col-lg-12 col-md-12 col-sm-12 col-xs-12 comments",
                    }).append(mediaElement);

                    subCommentContainer.prepend(commentElement)
                });

                //展开二级评论
                comments.addClass("in");
                //标记二级评论展开状态
                e.setAttribute("data-collapse", "in");
                e.classList.add("active");
            });
        }
    }
}

//展示标签
function showSelectTag() {
    $("#select-tag").show();
}

//选择标签
function selectTag(e) {
    var value = e.getAttribute("data-tag");
    var previous = $("#tag").val();
    if (previous.indexOf(value) == -1) {
        if (previous) {
            $("#tag").val(previous + ',' + value);
        } else {
            $("#tag").val(value);
        }
    }
}

$(function () {
    var editor = editormd("question-editor", {
        width: "100%",
        height: 500,
        path: "/js/lib/",
        delay: 0,
        watch: false ,
        placeholder: "请输入问题描述...",
        searchReplace: true,
        autofocus: false,
        imageUpload: true,
        imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
        imageUploadURL: "/file/upload",
        toolbarIcons : function() {
            return [
                "visible-lg",
                "undo", "redo", "|",
                "bold", "del", "italic", "quote", "|",
                "preview", "watch", "|",
                "link","image", "fullscreen", "|",
                "h1", "h2", "h3" , "h4", "|",
                "list-ul", "list-ol", "hr", "|",
                "code", "table", "preformatted-text", "clear"
            ]
        },
    });
});

/**
 * 删除问题
 */
function deleteQuestion(qid) {
    var id = qid.getAttribute("data-qid");
    if (confirm("此操作不可逆，你确定要删除吗？")) {
        $.ajax({
            type: "GET",
            url: "/blogs/delete/" + id,
            success: function () {
                alert("删除成功");
                window.location.href = "/manger/questions";
            }
        });
    }
}

/**
 * 未登录提问
 */
function unLoginPublish() {
    if (confirm("请先登录！")) {
        // window.location.href = "/login";
    }
}

$(function () {
    $('[data-toggle="tooltip"]').tooltip()
});

$(function removeClass() {
    if ($(window).width() < 800) {
        $("#contentMain").removeClass("main");
        $("#contentMain").addClass("main2");
    }
});

